<template>
  <div>
    <body id="poster">
      <el-form
        class="register-container"
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        label-position="left"
      >
        <h3 class="register_title">
          注册页面
          <el-button @click="toLogin()">去登录</el-button>
        </h3>
        <el-form-item label="" prop="loginName">
          <el-input
            type="text"
            v-model="ruleForm.loginName"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user-solid"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="name">
          <el-input
            type="text"
            v-model="ruleForm.name"
            placeholder="请输入网名"
            prefix-icon="el-icon-user-solid"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            placeholder="请输入密码"
            autocomplete="off"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            placeholder="请再次输入密码"
            autocomplete="off"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            style="background: #505458; border: none"
            >注册</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </body>
  </div>
</template>
<script>
export default {
  name: "RegisterS",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        loginName: "",
        name: "",
        password: "",
        checkPass: "",
      },
      rules: {
        loginName: [
          { required: true, message: "请输入你的用户名", trigger: "blur" },
          { min: 2, mxa: 9, message: "长度2-9个字符", trigger: "blur" },
        ],
        name: [
          { required: true, message: "请输入你的昵称", trigger: "blur" },
          { min: 2, mxa: 9, message: "长度2-9个字符", trigger: "blur" },
        ],
        password: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      console.log(this.ruleForm);
      this.$refs[formName].validate((valid) => {
        if (valid) {
        } else {
          return false;
        }
      });
      this.axios
        .post("http://localhost:8083/sys_user/register/",this.ruleForm)
        .then((response) => {
          let data = response.data;
          if (data.success) {
            this.ruleForm = {};
            this.$message({
              message: "注册成功,点击去登陆按钮 进行登录",
              type: "success",
            });
          }
        })
        .catch((error) => {
          this.$message({
            message: "用户已存在",
            type: "warning",
          });
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toLogin() {
      this.$router.push({ path: "/" });
    },
  },
};
</script>
<style scope>
#poster {
  background-position: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
  margin: 0px;
  padding: 0px;
  background-image: url("../assets/back.jpg");
}
.register-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 50px auto;
  width: 400px;
  height: 400px;
  padding: 15px 80px 0px 0px;
  background: rgb(90, 209, 209, 0.1);
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
.register_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>